package com.casic.ops.service.impl.pipeline;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.ops.dao.pipeline.PipelineNodeTemplateDAO;
import com.casic.ops.entity.domain.pipeline.PipelineNodeInfoDO;
import com.casic.ops.entity.domain.pipeline.PipelineNodeLoggerDO;
import com.casic.ops.entity.domain.pipeline.PipelineNodeTemplateDO;
import com.casic.ops.entity.dto.pipeline.PipelineNodeInfoDTO;
import com.casic.ops.entity.dto.pipeline.PipelineNodeLoggerDTO;
import com.casic.ops.entity.dto.pipeline.PipelineNodeTemplateDTO;
import com.casic.ops.entity.request.pipeline.CommonPageRequest;
import com.casic.ops.entity.request.pipeline.PipelineNodeTemplateRequest;
import com.casic.ops.entity.vo.pipeline.response.MultiResponse;
import com.casic.ops.entity.vo.pipeline.response.PageResponse;
import com.casic.ops.entity.vo.pipeline.response.Response;
import com.casic.ops.entity.vo.pipeline.response.SingleResponse;
import com.casic.ops.service.api.pipeline.PipelineNodeTemplateService;
import com.casic.ops.utils.pipeline.PipelineNodeTemplateConvertor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PipelineNodeTemplateServiceImpl implements PipelineNodeTemplateService {

    public PipelineNodeTemplateServiceImpl() {
        super();
    }

    @Resource
    private PipelineNodeTemplateDAO pipelineNodeTemplateDAO;

    @Resource
    private PipelineNodeTemplateConvertor pipelineNodeTemplateConvertor;

    @Override
    public Response add(PipelineNodeTemplateRequest pipelineNodeTemplateRequest) {
        PipelineNodeTemplateDTO pipelineNodeTemplateDTO=pipelineNodeTemplateRequest.getPipelineNodeTemplateDTO();
        pipelineNodeTemplateDTO.init();
        int result=pipelineNodeTemplateDAO.insert(pipelineNodeTemplateConvertor.toDO(pipelineNodeTemplateDTO));
        if(result==0){
            return Response.error("新增失败");
        }else {
            return Response.success();
        }
    }

    @Override
    public Response update(PipelineNodeTemplateRequest pipelineNodeTemplateRequest) {
        int result=pipelineNodeTemplateDAO.updateById(pipelineNodeTemplateConvertor.toDO(pipelineNodeTemplateRequest.getPipelineNodeTemplateDTO()));
        if(result==0){
            return Response.error("更新失败");
        }else{
            return Response.success();
        }
    }

    @Override
    public Response delete(Long[] ids) {
        /**
         * 逻辑删除，0为已删，1为未删
         */
        for(Long id:ids){
            PipelineNodeTemplateDO pipelineNodeTemplateDO=pipelineNodeTemplateDAO.selectById(id);
            if(pipelineNodeTemplateDO!=null){
                pipelineNodeTemplateDO.setDeleted("0");
                pipelineNodeTemplateDAO.updateById(pipelineNodeTemplateDO);
            }
        }
        return Response.success();
    }

    @Override
    public SingleResponse<PipelineNodeTemplateDTO> getById(Long id) {
        PipelineNodeTemplateDO pipelineNodeTemplateDO=pipelineNodeTemplateDAO.selectById(id);
        if(pipelineNodeTemplateDO!=null){
            return SingleResponse.of(pipelineNodeTemplateConvertor.toDTO(pipelineNodeTemplateDO));
        }else{
            return null;
        }
    }

    @Override
    public MultiResponse<PipelineNodeTemplateDTO> list() {
        QueryWrapper<PipelineNodeTemplateDO> qw=new QueryWrapper<>();
        qw.eq("deleted","1");
        List<PipelineNodeTemplateDO> pipelineNodeTemplateDOList=pipelineNodeTemplateDAO.selectList(qw);
        int size=pipelineNodeTemplateDOList.size();
        System.out.println(size);
        System.out.println(pipelineNodeTemplateDOList);
        List<PipelineNodeTemplateDTO> pipelineNodeTemplateDTOList=pipelineNodeTemplateConvertor.toDTOList(pipelineNodeTemplateDOList);
        /**
         * 构建递归树
         */
        List<PipelineNodeTemplateDTO> rootNode = getRootNode(pipelineNodeTemplateDTOList);
        rootNode.forEach(v-> setChildren(v,pipelineNodeTemplateDTOList));
        System.out.println(rootNode);
        return MultiResponse.of(rootNode);
    }

    @Override
    public PageResponse<PipelineNodeTemplateDTO> page(CommonPageRequest request) {

        QueryWrapper<PipelineNodeTemplateDO> qw=new QueryWrapper();
        Page<PipelineNodeTemplateDO> page=new Page(request.getPageIndex(),request.getPageSize());
        IPage<PipelineNodeTemplateDO> iPage=pipelineNodeTemplateDAO.selectPage(page,qw);
        /**
         * 查询总数
         */
        Integer count=pipelineNodeTemplateDAO.selectCount(qw);
        PageResponse<PipelineNodeTemplateDTO> response=new PageResponse<>();
        response.setTotalCount(count);
        List<PipelineNodeTemplateDTO> pipelineNodeTemplateDTOList=pipelineNodeTemplateConvertor.toDTOList(iPage.getRecords());
        response.setData(pipelineNodeTemplateDTOList);
        return response;
    }

    private List<PipelineNodeTemplateDTO> getRootNode(List<PipelineNodeTemplateDTO> pipelineNodeDTOList) {
        List<PipelineNodeTemplateDTO> ret = new ArrayList<>();
        for (PipelineNodeTemplateDTO node:pipelineNodeDTOList){
            System.out.println(node);
            if (node.getType().intValue()==0) {
                ret.add(node);
            }
        }
        return ret;
    }

    private void setChildren(PipelineNodeTemplateDTO node, List<PipelineNodeTemplateDTO> pipelineNodeDTOList) {
        List<PipelineNodeTemplateDTO> childList = new ArrayList<>();
        pipelineNodeDTOList.forEach(v->{
            if (node.getUuid()==(v.getParentUuid()))childList.add(v.setChildren(null));
        });
        node.setChildren(childList);
    }
}
